package com.jinzhi.jzweb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jinzhi.jzweb.domain.cond.CertificateInfoCond;
import com.jinzhi.jzweb.domain.vo.CertificateInfoVo;
import com.jinzhi.sys.domain.UserDO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jinzhi.jzweb.dao.CertificateInfoDao;
import com.jinzhi.jzweb.domain.CertificateInfoDO;
import com.jinzhi.jzweb.service.CertificateInfoService;
import com.jinzhi.common.base.CoreServiceImpl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2024-05-15 17:22:11 | lance</small>
 */
@Service
public class CertificateInfoServiceImpl extends CoreServiceImpl<CertificateInfoDao, CertificateInfoDO> implements CertificateInfoService {

    @Autowired
    private CertificateInfoDao certificateInfoDao;

    @Override
    public String saveUserCertificate(UserDO user, CertificateInfoCond cond) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate parse = LocalDate.parse(cond.getCertificateTime(), formatter);

        CertificateInfoDO certificateInfoDO1 = this.selectOne(new EntityWrapper<CertificateInfoDO>().eq("career", cond.getCareer()).eq("level", cond.getLevel()).eq("info_user_id",user.getId()));
        if (certificateInfoDO1!=null){
            return "已添加了该证书！！！";
        }
        CertificateInfoDO certificateInfoDO=new CertificateInfoDO();
        BeanUtils.copyProperties(cond,certificateInfoDO);
        certificateInfoDO.setInfoUserId(user.getId());
        certificateInfoDO.setCertificateTime(parse);
        certificateInfoDao.insert(certificateInfoDO);
        return null;
    }

    @Override
    public void updateUserCertificate(CertificateInfoCond cond) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDateTime = LocalDate.parse(cond.getCertificateTime(), formatter);
        CertificateInfoDO certificateInfoDO = this.selectById(cond.getId());
        certificateInfoDO.setCertificateTime(localDateTime);
        certificateInfoDO.setPhoto(cond.getPhoto());
        certificateInfoDO.setCareer(cond.getCareer());
        certificateInfoDO.setLevel(cond.getLevel());
        certificateInfoDO.setNumber(cond.getNumber());
        certificateInfoDao.updateById(certificateInfoDO);
    }

    @Override
    public List<CertificateInfoVo> getUserCertificate(UserDO user) {
        List<CertificateInfoDO> infoUserId = this.selectList(new EntityWrapper<CertificateInfoDO>().eq("info_user_id", user.getId()));
        List<CertificateInfoVo> voList=new ArrayList<>();

        infoUserId.forEach(e->{
            CertificateInfoVo vo=new CertificateInfoVo();
            BeanUtils.copyProperties(e,vo);
            voList.add(vo);
        });
        return voList;
    }
}
